function addPoint(){
	if(currentMap.mapProvider == 'geoserver'){
		return;
	}
	 var center = currentMap.getCenter(); // GPS_LatLng
	 var pos = new GPS_LatLng(center.lat(), center.lng());
	 var tip = new GPS_Tip('', pos);
}

/**初始化兴趣点,需要移除之前所有的兴趣点
 * @return
 */
function initInterestPoints(){

	removeAllInterest();
	
	Ext.Ajax.request({
	    url: './get_interest.jsp?time='+(new Date()),
	    params: '',
	    success:function(res){
			if(res.status == 404 ||
					res.responseText.indexOf('setLoginInfo') != -1){
				top.location.href = '/';
				return;
			}
			var resp = res.responseText;
			try{
				interestPoints = eval(resp);
				for(var i = 0; i < interestPoints.length; i++){
					var point = interestPoints[i];
					var pos = new GPS_LatLng(point.lat, point.lng);
					try{
						new GPS_Tip(point.text, pos, point.id);
					}catch(e){
						if(console)
							console.log(e);
					}
				}
				if(currentMap.mapProvider == 'geoserver'){ // geo本地地图
					var handler = {"zoomend" : packInterest};
					currentMap.map.events.on(handler);
				}
			}catch(e){
				if(console)
					console.log(e);
			}
		}
	});
}

/**将标签和箭头图片合并
 * @return
 */
function packInterest(){
	if(currentMap.mapProvider == 'geoserver' && gpsTips && gpsTips.length > 0){ // for interest point
	    for(var i = 0; i < gpsTips.length; i++){
	    	var interest = gpsTips[i];
	    	interest.tipLayer.move(interest.pos, -50, -70);
	    }
    }
}

/**移除所有的兴趣点
 * @return
 */
function removeAllInterest(){
	for(var i = 0 ; i < gpsTips.length; i++){
		if(gpsTips[i].id == id){
			gpsTips[i].remove();
		}
	}
	gpsTips = [];
}

var interestPoints = [];
var gpsTips = [];

var GPS_Tip = function(text, pos /*GPS_LatLng*/, id){
	this.id = id ? id : ("id_"+Math.random()*1000);
	this.text = text;
	this.pos = pos;
	
	var tipId = this.id;
	var self = this;
	var t = new GPS_Icon();
    t.iconSize = new GPS_Size(140, 90); // new GPS_Size(32, 16)
    t.iconAnchor = new GPS_Point(70, 90);
    t.shadow = "";
    t.image = 'images/tip/tip.png';// "images/car1_gray.png";
    var markerOptions = {
        icon: t , title:'',draggable:true
    };
    var marker = new GPS_Marker(pos, markerOptions, currentMap.map);//new GPS_Marker(s, markerOptions);
	currentMap.addOverlay(marker);
	
	var tip = null;
	if(currentMap.mapProvider == 'geoserver'){ // geo本地地图
		tip = new GPS_Label(pos, currentMap.map);
		currentMap.addLabel(tip);
		tip.put_text(text);
		tip.move(pos, -50, -70);
		tip.showMe();
	}else{
		tip = new TipText(pos, text, currentMap.map, new GPS_Size(134, 85));
	}
	this.tipLayer = tip;
	
	if(currentMap.mapProvider != 'geoserver'){
		marker.addListener("click",function(marker, p/*GPS_LatLng*/){
	    	var g_marker_prompt = '<div>'+interestText+': <input onclick="this.focus();" type="text" value="'+self.text+'" id="'+tipId+'" size="30"/></div>'+
	    	'<div style="margin-top:15px"><input type="button" onclick="setInterestText(\''+tipId+'\')" value="'+interestOk+'"/>'+
	    	'<input type="button" value="'+interestDelete+'" onclick="deleteInterest(\''+tipId+'\');"/></div>';
	    	marker.openInfoWindowHtml(g_marker_prompt, {maxWidth:130, onCloseFn:function(){
	    		
	        }, lat:p.lat(), lng:p.lng()});
	    });
		if(!id){
			var g_marker_prompt = '<div>'+interestText+': <input onclick="this.focus();" type="text" value="'+self.text+'" id="'+tipId+'" size="30"/></div>'+
	    	'<div style="margin-top:15px"><input type="button" onclick="setInterestText(\''+tipId+'\')" value="'+interestOk+'"/>'+
	    	'<input type="button" value="'+interestDelete+'" onclick="deleteInterest(\''+tipId+'\');"/></div>';
	    	marker.openInfoWindowHtml(g_marker_prompt, {maxWidth:130, onCloseFn:function(){
	    		
	        }, lat:pos.lat(), lng:pos.lng()});
		}
		
		marker.addListener("dragstart",function(marker, p/*GPS_LatLng*/){
	    	tip.setVisible(false);
	    });
		marker.addListener("dragend",function(marker, p/*GPS_LatLng*/){
			tip.move(p);
	    	tip.setVisible(true);
	    	Ext.Ajax.request({
	    	    url: './set_interest.jsp',
	    	    params: 'lat='+p.lat()+"&lng="+p.lng()+"&text="+self.text+"&id="+self.id+'&time='+(new Date()),
	    	    success:function(res){
	    			if(res.status == 404 ||
	    					res.responseText.indexOf('setLoginInfo') != -1){
	    				top.location.href = '/';
	    				return;
	    			}}
	    	});
	    });
	}
	this.marker = marker;
	this.marker.show();
	gpsTips.push(this);
};

GPS_Tip.prototype.setText = function(text){
	this.text = text;
	var marker = this.marker;
	var pos = marker.getPoint();
	var self = this;
	Ext.Ajax.request({
	    url: './set_interest.jsp',
	    params: 'lat='+pos.lat()+"&lng="+pos.lng()+"&text="+text+"&id="+self.id+'&time='+(new Date()),
	    success:function(res){
			if(res.status == 404 ||
					res.responseText.indexOf('setLoginInfo') != -1){
				top.location.href = '/';
				return;
			}}
	});
	this.marker.closeInfoWindow();
	if(this.tipLayer instanceof GPS_Label)
		this.tipLayer.put_text(text);
	else
		this.tipLayer.setText(text);
};

GPS_Tip.prototype.remove = function(){
	this.marker.closeInfoWindow();
	this.marker.remove();
	if(this.tipLayer instanceof GPS_Label)
		this.tipLayer.destroy();//
	else
		this.tipLayer.removeFromMap();
};

function setInterestText(id){
	for(var i = 0 ; i < gpsTips.length; i++){
		if(gpsTips[i].id == id){
			gpsTips[i].setText(document.getElementById(id).value);
			break;
		}
	}
}

function deleteInterest(id){
	for(var i = 0 ; i < gpsTips.length; i++){
		if(gpsTips[i].id == id){
			gpsTips[i].remove();
			gpsTips.splice(i, 1);
			break;
		}
	}
	Ext.Ajax.request({
	    url: './del_interest.jsp',
	    params: 'id='+id+'&time='+(new Date()),
	    success:function(res){
			if(res.status == 404 ||
					res.responseText.indexOf('setLoginInfo') != -1){
				top.location.href = '/';
				return;
			}}
	});
}

TipText.prototype = new google.maps.OverlayView();

function TipText(pos /*GPS_LatLng*/, text, map, size /*GPS_Size*/) {

    // Now initialize all properties.
    this.pos_ = pos;
    this.text_ = text;
    this.map_ = map;

    // We define a property to hold the image's div. We'll 
    // actually create this div upon receipt of the onAdd() 
    // method so we'll leave it null for now.
    this.div_ = null;
    this.textSize_ = size;

    // Explicitly call setMap on this overlay
    this.setMap(map);
  }

  TipText.prototype.onAdd = function() {

    // Note: an overlay's receipt of onAdd() indicates that
    // the map's panes are now available for attaching
    // the overlay to the map via the DOM.

    // Create the DIV and set some basic attributes.
    var div = document.createElement('DIV');
    div.style.borderStyle = "none";
    div.style.borderWidth = "0px";
    div.style.position = "absolute";
    div.style.textAlign = "left";
    div.style.color = "blue";
    div.style.width = this.textSize_.width +'px';
    div.style.height = this.textSize_.height + 'px';
    div.innerHTML = this.text_;

    // Set the overlay's div_ property to this DIV
    this.div_ = div;

    // We add an overlay to a map via one of the map's panes.
    // We'll add this overlay to the overlayImage pane.
    var panes = this.getPanes();
    panes.overlayImage.appendChild(div);
    div.style.zIndex = 1500;
  };

  TipText.prototype.move = function(pos/*GPS_LatLng*/){
	  this.pos_ = pos;
	  this.draw();
  };
  
  TipText.prototype.setText = function(text){
	  this.div_.innerHTML = text;
  };
  
  TipText.prototype.setVisible = function(display){
	  if(display){
		  this.div_.style.display='block';
	  }else{
		  this.div_.style.display='none';
	  }
	  
  };
  
  
  TipText.prototype.draw = function() {

    // Size and position the overlay. We use a southwest and northeast
    // position of the overlay to peg it to the correct position and size.
    // We need to retrieve the projection from this overlay to do this.
    var overlayProjection = this.getProjection();

    // Retrieve the southwest and northeast coordinates of this overlay
    // in latlngs and convert them to pixels coordinates.
    // We'll use these coordinates to resize the DIV.
    var sw = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(this.pos_.lat(), this.pos_.lng()));

    // Resize the image's DIV to fit the indicated dimensions.
    var div = this.div_;
    div.style.left = (sw.x - this.textSize_.width/2) + 'px';
    div.style.top = (sw.y - this.textSize_.height) + 'px';
//    div.style.width = (ne.x - sw.x) + 'px';
//    div.style.height = (sw.y - ne.y) + 'px';
  };

  TipText.prototype.onRemove = function() {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  };
  	TipText.prototype.removeFromMap = function() {
	    this.setMap(null);
	  };
  
  
 